<?php
defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	class ArticleModel extends Model {
		private $queryGetRow = "";
		public function display( $start=0 , $perpage=20 , $searchString="" , $searchColumns = array("title","content") , $sortColumn="title" , $sortCriteria="ASC" ){
			
			$start = is_numeric($start)&&$start>-1?$start*$perpage:0;
			$perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
			
			$result = array();
			$query = "SELECT articles.id as article_id , title , resource_id , resources.name as resource_name , articles.published as article_published, articles.created , users.first_name AS author  FROM articles , resources , users WHERE articles.resource_id = resources.id AND articles.user_id = users.id AND resources.published=1 " ;
			if ( $searchString != "" ){
				$searchColumns = ( $searchColumns===false || ( is_array($searchColumns) && empty($searchColumns) ) ) ? array("title","content") : $searchColumns ; 
				if ( is_array($searchColumns) && !empty($searchColumns) ){
				$conditions = "" ;
					$conditions = array() ;
					foreach( $searchColumns AS $columnName ){
						$conditions[] = " $columnName LIKE '%$searchString%' " ;
					}
					$conditions = " AND (" . implode("OR", $conditions ) . ") ";
					$sortColumn = $sortColumn===""||$sortColumn===false?"title":$sortColumn;
					$sortCriteria = $sortCriteria===""||$sortCriteria===false?"ASC":$sortCriteria;
					$query .= "$conditions " ;
				}
			}
			if( $sortColumn != "" || $sortColumn != false ){
				$sortCriteria = $sortCriteria!=""&&$sortCriteria!=false?$sortCriteria:"ASC";
				$query .= " ORDER BY $sortColumn $sortCriteria " ;
			}
			$this->queryGetRow = $query ;
			$query .= " LIMIT $start , ".$perpage."; " ;
			echo $query;
			$result = $this->executeQueryAsObject( $query ) ;
			return $result;
		}
		public function getTotalRecords(){
			return $this->getRowWithQuery($this->queryGetRow) ;
		}
		public function add( $title , $resource , $published , $content , $user_id ){
			$time = date("Y-m-d");
			return $this->executeQuery( "INSERT INTO articles SET title='$title' , user_id = '$user_id' , content='$content' , resource_id='$resource' , published='$published', created='$time' , modified='$time' ;");
		}
		public function edit( $id , $title , $resource , $published , $content , $user_id ){
			$time = date("Y-m-d");
			return $this->executeQuery( "UPDATE articles SET title='$title' , user_id = '$user_id' , content='$content' , resource_id='$resource' , published='$published', modified='$time' WHERE articles.id='$id';");
		}
		public function delete( $id ){
			$this->executeQuery( "DELETE FROM articles WHERE id='$id';");
		}
		public function find( $id ){
			return $this->executeQueryAsObject("SELECT articles.id as article_id , title , resource_id , resources.name as resource_name , articles.published as article_published, articles.created , articles.content , users.first_name AS author FROM users , articles , resources WHERE articles.resource_id = resources.id AND users.id = articles.user_id AND resources.published=1 AND articles.id = '$id' ;");
		}

	}
?>